package com.panghl.task;

import com.alipay.api.AlipayApiException;
import com.panghl.entity.OrderInfo;
import com.panghl.enums.PayType;
import com.panghl.service.AliPayService;
import com.panghl.service.OrderInfoService;
import com.panghl.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@Component
public class AliPayTask {



    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private AliPayService aliPayService;

    /**
     * 从第0秒开始每隔30秒执行一次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron = "1/30 * * * * ?")
    public void orderConfirm() throws IOException, AlipayApiException {
        log.info("orderConfirm 被執行....");

        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayType.ALIPAY.getType());
        for (OrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单====> {}",orderNo);

            //核实订单状态，调用微信支付查单接口
            aliPayService.checkOrderStatus(orderNo);
        }
    }

    public static void main(String[] args) {
        Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).minus(5, ChronoUnit.MINUTES);
        Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).minus(Duration.ofMinutes(5));
        System.out.println(now);
        System.out.println(now2);
    }



}
